<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Platform\System\Tools\ResultTool;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * @param Exception $exception
     * @return mixed|void
     * @throws Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        // 如果config配置debug为true ==>debug模式的话让laravel自行处理
//        $debug_status = config('app.debug'); // .env文件配置
//        if($debug_status){
//            return parent::render($request, $exception);
//        }
        return $this->handle($request, $exception);
    }

    /**
     * 异常接管
     * @param $request
     * @param Exception $exception
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function handle($request, Exception $exception){
        //如果是接口请求，则抛出json
        if($request->is('api/*')) {
            // 只处理自定义的APIException异常
        }

        if( $exception instanceof ApiException ) {  //此处写ApiException文件所处路径
            $result = [
                'code' => $exception->errorCode ,
                'message' => $exception->errorMsg ,
            ];
            return response()->json($result,ResultTool::CODE_SUCCESS);
        }

        # 此处可以写多个自定义异常类
        /***
        if($exception  instanceof   \APP\Exceptions\otherException ) {
            $result = [
                "status" => 2 , //操作状态： 1 成功 2 失败
                "errorCode"=>$exception->errorCode,
                "msg"    => $exception->errorMsg,
                "result"   => '',
            ];
            return response()->json($result);
        }
         * ***/

        # 继续写其他自定义异常
        /***
         * code
         *
         * ***/
        return parent::render($request, $exception);
    }

}
